package org.hot100_2;

import org.leetcode.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class leetcode199 {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> res= new ArrayList<>();
        method(root,res);

        return res;
    }

    private void method(TreeNode root,List<Integer> res){

        if (root==null){
            return;
        }

        Queue<TreeNode> queue = new LinkedList<>();

        queue.add(root);

        while (!queue.isEmpty()){
            int size = queue.size();
            List<Integer> item = new ArrayList<>();
            while (size>0){
                TreeNode node = queue.poll();
                item.add(node.val);
                if (node.left!=null){
                    queue.add(node.left);
                }

                if (node.right!=null){
                    queue.add(node.right);
                }
                size--;
            }
            res.add(item.get(item.size()-1));
        }

    }
}
